package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsPath;
import com.yidu.logisticssystem.entity.LogisticsSite;
import com.yidu.logisticssystem.service.LogisticsPathService;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 路线表(LogisticsPath)表控制层
 *
 * @author makejava
 * @since 2021-03-08 16:46:51
 */
@Api(description= "线路")
@RestController
@RequestMapping("logisticsPath")
public class LogisticsPathController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsPathService logisticsPathService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsPath selectOne(Integer id) {
        return this.logisticsPathService.queryById(id);
    }

    /**
     * 分页查询所有
     * @param page
     * @param limit
     * @return
     */

    @ApiOperation(value = "查询所有的线路",notes = "返回参数")
    @GetMapping
    public Map<String,Object> findAll(LogisticsPath logisticsPath, int page, int limit){
        //将参数传入分页助手
        PageHelper.startPage(page,limit);

        Map<String,Object> map = new HashMap<>();

        List<LogisticsPath> list =logisticsPathService.queryAll(logisticsPath);

        PageInfo<LogisticsPath> pageInfo = new PageInfo<>(list);

        map.put("code",0);
        map.put("msg","");
        map.put("data",pageInfo.getList());//所有数据
        map.put("count",pageInfo.getTotal());//总行数

        return map;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除",notes = "返回参数")
    @PostMapping("delete/{id}")
    public BaseMessage delete(@PathVariable int id){
        boolean flag=logisticsPathService.deleteById(id);

        if(flag){
            return BaseMessage.succes();
        }else{
            return BaseMessage.fail();
        }
    }

    /**
     * 新增和修改
     * @param logisticsPath
     * @return
     */
    @ApiOperation(value = "修改和添加功能实现",notes = "返回参数")
    @PostMapping("addAndUpdate")
    public BaseMessage addAndUpdate(@RequestBody LogisticsPath logisticsPath){
        BaseMessage baseMessage = new BaseMessage();
        if(logisticsPath.getPathId()==null){
            logisticsPathService.insert(logisticsPath);
            baseMessage.setState(1);
            baseMessage.setMessage("增加成功");
        }else{

            /**
             * 在页面上显示单条数据
             */
            LogisticsPath logisticsPath1 = logisticsPathService.queryById(logisticsPath.getPathId());

            Map<String,Object> map = new HashMap<>();
            map.put("code",0);
            map.put("msg","");
            map.put("data",logisticsPath);//所有数据
            map.put("count","");//总行数


            /**
             * 修改数据
             */
            logisticsPathService.update(logisticsPath);
            baseMessage.setState(1);
            baseMessage.setMessage("修改成功");
        }
        return baseMessage;
    }

}
